import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Set;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import uk.ac.ebi.ontocat.OntologyService;
import uk.ac.ebi.ontocat.OntologyServiceException;
import uk.ac.ebi.ontocat.OntologyTerm;
import uk.ac.ebi.ontocat.bioportal.BioportalOntologyService;
import uk.ac.ebi.ontocat.file.FileOntologyService;

public class SVNFetch {

	public static void main(String[] args) throws SVNException,
	OntologyServiceException, URISyntaxException {

		new BioportalOntologyService();
		// .parseURIDecoded("http://efo.svn.sourceforge.net/viewvc/efo/trunk/src/efoinowl/");
		DAVRepositoryFactory.setup();

		String url = "http://efo.svn.sourceforge.net/svnroot/efo/trunk/src/efoinowl/efo.owl";
		String name = "anonymous";
		String password = "anonymous";
		long startRevision = 0;
		long endRevision = -1; // HEAD (the latest) revision

		SVNRepository repository = null;

		repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
		ISVNAuthenticationManager authManager = SVNWCUtil
		.createDefaultAuthenticationManager(name, password);
		repository.setAuthenticationManager(authManager);

		Collection<SVNLogEntry> logEntries = null;

		logEntries = repository.log(new String[] { "" }, null, startRevision,
				endRevision, true, true);

		System.out
		.println("DATE\tREVISION\tTOTAL_CLASSES\tEFO_CLASSES\tSYNONYMS\tDEFINITIONS\tOBSOLETED");
		for (SVNLogEntry entry : logEntries) {
			URI uri = new URI(
					"http://efo.svn.sourceforge.net/viewvc/efo/trunk/src/efoinowl/efo.owl?revision=" + entry
					.getRevision());
			OntologyService os = new FileOntologyService(uri);
			Set<OntologyTerm> terms = os.getAllTerms(null);
			Integer totalTerms = terms.size();
			Integer efoClasses = 0;
			Integer synonymCount = 0;
			Integer defCount = 0;
			Integer obsCount = 0;
			for (OntologyTerm ot : terms) {
				if (ot.getAccession().startsWith("EFO")) {
					efoClasses++;
				}
				if (ot.getLabel().startsWith("obsol")) {
					obsCount++;
				}
				synonymCount += os.getSynonyms(ot).size();
				defCount += os.getDefinitions(ot).size();
				// if (os.getAnnotations(ot).get("definition_editor") != null) {
				// for (String annot : os.getAnnotations(ot).get(
				// "definition_editor")) {
				// if (annot.contains("Adamusiak")) {
				// System.out.println("Adamusiak joined on " + entry
				// .getRevision()
				// + "\t"
				// + entry.getDate());
				// System.exit(0);
				// }
				// }
				// }
			}

			System.out.println(entry.getDate().toGMTString() + "\t"
					+ entry.getRevision()
					+ "\t"
					+ totalTerms
					+ "\t"
					+ efoClasses
					+ "\t"
					+ synonymCount
					+ "\t"
					+ defCount
					+ "\t"
					+ obsCount);

		}
	}
}
